<?php
/*
	$table		bang de insert
	$id
	$labels		danh sach cac nhan se hien thi tuong ung voi moi truong, chi so phai bat dau tu 0
	$columns	danh sach cac cot se hien thi, neu khong cho tham so nay se hien thi tat ca,
					chi so phai bat dau tu 0
	$imagecolumns		danh sach cac truong la duong dan file anh
	$uploadfile cac truong la file upload
	$thumbnailColumns
	$combocolumns		la mang 2 chieu, moi phan tu co key la ten cot, value la mot mang chua cac phan tu cua combo
	$page		trang ma sau khi sua se quay tro lai
*/
$CI = &get_instance();
$CI->load->helper('html');
$CI->load->helper('form');
$CI->load->database();
$query = $CI->db->query('show columns from ' . $table);
$allColumns = $query->result();
if(!isset($columns))
{
	foreach($allColumns as $row)
		$columns[] = $row->Field;
}
if(!isset($labels))
	foreach($columns as $field)
		$labels[] = $field;
if(count($labels) < count($columns))
{
	$n1 = count($labels);
	$n2 = count($columns);
	for($i=0; $i<$n2-$n1; $i++)
		$labels[] = $columns[$n1+$i];
}
//// lay cac thong tin trong csdl
	// tim khoa chinh cua bang
	$keyFields = array();
	foreach($allColumns as $row)
		if($row->Key == 'PRI')
			$keyFields[$row->Field] = $row->Field;
	// ket thuc tim khoa chinh
	if(count($keyFields) == 1)
	{
		foreach($keyFields as $key=>$value)
		{
			$CI->db->where($key, $id);
		}
	}
	else
	{
		$i = 0;
		foreach($keyFields as $key=>$value)
		{
			$CI->db->where($key, $id[$i]);
			$i++;
		}
	}
	
	$query = $CI->db->get($table);
	foreach($query->result_array() as $row)
	{
		$oldData = $row;
	}
	if(!isset($oldData))
		show_error('id khong chinh xac');
//	print_r($oldData);
//// ket thuc lay thong tin trong csdl
function getColumnProp($column, &$allColumns)
{
	foreach($allColumns as $r)
		if($column == $r->Field)
			return $r;
}
function isImageColumn($column, &$imagecolumns)
{
	if(!isset($imagecolumns))
	{
		return FALSE;
	}
	else
	{
		foreach($imagecolumns as $r)
			if($r == $column)
				return TRUE;
	}
	return FALSE;
}
function isUploadFile($column, &$m_uploadFlie){
	if(!isset($m_uploadFlie))
	{
		return FALSE;
	}
	else
	{
		foreach($m_uploadFlie as $r)
			if($r == $column)
				return TRUE;
	}
	return FALSE;
}
function isThumbnailColumn($column, &$thumbnailColumns)
{
	if(!isset($thumbnailColumns))
		return FALSE;
	if(isset($thumbnailColumns[$column]))
		return TRUE;
	return false;
}
function isComboColumn($column, &$combocolumns)
{
	if(!isset($combocolumns))
		return FALSE;
	else
	{
		foreach($combocolumns as $key=>$value)
			if($key == $column)
				return TRUE;
	}
	return FALSE;
}
?>
<script type="text/javascript" src="javascript/admin/script.js">
</script>
<div>
<?php echo validation_errors(); ?>
</div>
<div>
<table width="100%">
<?php
	echo form_open_multipart('admin/quanly/thuchiensua/'.$table.'/'.$page.'/'.$id, array('name'=>$table, 'id'=>$table));
	$n = count($columns);
	for($i=0; $i<$n; $i++):?>
		<tr>
        	<td>
				<div><a class="link">
                	<?php 
					if(!isThumbnailColumn($columns[$i], $thumbnailcolumns))
						echo $labels[$i]; 
					?>
                    </a>
				</div>
				<div>
                	<?php 
						$prop = getColumnProp($columns[$i],$allColumns);
						if($prop->Extra == 'auto_increment')
						{
							$data = array(
										  'name'=>$columns[$i],
										  'id'=>$columns[$i],
										  'maxlength'=>'15',
										  'size'=>'20',
										  'readonly'=>"readonly",
										  'value'=>$oldData[$columns[$i]]
										  );
							echo form_input($data);
						}
						else if(isImageColumn($columns[$i],$imagecolumns))
						{
							echo '<table>';
							echo '<tr>';
							echo '<td><img src="'.$oldData[$columns[$i]].'" width="32" height="32"/></td>';
							echo '<td>'.form_upload(array('name'=>$columns[$i],'id'=>$columns[$i])).'</td>';
							//echo '<td><a href="'.base_url().'/upload.php" target="_blank">Chọn file</a></td>';
							echo '</tr>';
							///echo '<tr><td colspan="2"><input type="text" name="'.$columns[$i].'" id="'.$columns[$i].'"></td></tr>';
							echo '</table>';
						}
						else if(isUploadFile($columns[$i], $uploadfile)){
							echo '<table>';
							echo '<tr>';
							//echo '<td><img src="'.$oldData[$columns[$i]].'" width="32" height="32"/></td>';
							//echo '<td>'.form_upload(array('name'=>$columns[$i],'id'=>$columns[$i])).'</td>';
							//echo '<td></td>';
							echo '</tr>';
							echo '<tr><td colspan="1"><input size="50" readonly="readonly" type="text" value="'.$oldData[$columns[$i]].'" name="'.$columns[$i].'" id="'.$columns[$i].'"><input type="button"  onclick="openUploadFileForm(\''.$columns[$i].'\',\''.base_url().'\upload.php?files=\');" value="Chọn file"/> <input type="button" onclick="xoafile(\''.$columns[$i].'\')" value="Xóa"></td></tr>';
							echo '</table>';
						}
						
						else if(isThumbnailColumn($columns[$i], $thumbnailcolumns))
						{
						}
						else if(isComboColumn($columns[$i], $combocolumns))
						{
							$option = $combocolumns[$columns[$i]];
							
//							echo form_dropdown($columns[$i], $option, $oldData[$columns[$i]]);
							echo form_dropdown($columns[$i], $option, set_combo_value($columns[$i], $oldData[$columns[$i]]));
						}
						else if(strpos($prop->Type,'int') === 0 || strpos($prop->Type, 'double') === 0 || strpos($prop->Type, 'float')===0)
						{
							$data = array(
										  'name'=>$columns[$i],
										  'id'=>$columns[$i],
										  'maxlength'=>'15',
										  'size'=>'20',
//										  'value'=>$oldData[$columns[$i]]
										  );
							$data['value'] = set_value($columns[$i], $oldData[$columns[$i]]);
							echo form_input($data);
						}
						else if(strpos($prop->Type,'tinyint') === 0)
						{
							if(!isset($_POST['submit_'.$table]))
							{
								echo form_checkbox($columns[$i], $columns[$i], $oldData[$columns[$i]]);
							}
							else
							{
								echo form_checkbox($columns[$i], $columns[$i], isset($_POST[$columns[$i]]));
							}
						}
						else if(strpos($prop->Type,'bigint') === 0)
						{
							$data = array(
										  'name'=>$columns[$i],
										  'id'=>$columns[$i],
										  'maxlength'=>'20',
										  'size'=>'25',
//										  'value'=>$oldData[$columns[$i]]
										  );
							$data['value'] = set_value($columns[$i], $oldData[$columns[$i]]);
							echo form_input($data);
						}
						else if(strpos($prop->Type,'varchar') === 0)
						{
							$data = array(
										  'name'=>$columns[$i],
										  'id'=>$columns[$i],
										  'size'=>'50',
//										  'value'=>$oldData[$columns[$i]]
										  );
							$data['value'] = set_value($columns[$i], $oldData[$columns[$i]]);
							echo form_input($data);
						}
						else if(strpos($prop->Type,'text') === 0)
						{
							echo '<input type="button" value="Chèn ảnh" onclick="openUploadImageForm(\''.$columns[$i].'\',\''. site_url('admin/quanly/uploadimage') .'\');"/>';
							$data = array(
										  'name'=>$columns[$i],
										  'id'=>$columns[$i],
										  'rows'=>'10',
										  'cols'=>'80',
//										  'value'=>$oldData[$columns[$i]]
										  );
							$data['value'] = set_value($columns[$i], $oldData[$columns[$i]]);
							echo form_textarea($data);
						}
						else if(strpos($prop->Type,'datetime') === 0 || strpos($prop->Type, 'date') === 0)
						{
							if($oldData[$columns[$i]])
							{
							$datetime = split(' ', $oldData[$columns[$i]]);
							$date = $datetime[0];
//							$time = $datetime[1];
							$temp = split('-', $date);
							$year = $temp[0];
							$month = $temp[1];
							$day = $temp[2];
							$data = array(
										  'name'=>$columns[$i],
//										  'value'=>mktime(0, 0, 0, $month, $day, $year)
									);
							$data['value'] = set_form_date_value($columns[$i], mktime(0, 0, 0, $month, $day, $year));
							}
							else
							{
								$data = array(
										  'name'=>$columns[$i],
										  );
								$data['value'] = mktime(0, 0, 0, 1, 1, 1970);
							}
							echo form_date($data);
						}
					?>
        		</div>
			</td>
        </tr>
<?php 
	endfor;
?>
		<tr>
        	<td>
            	<div align="center">
                	<?php
					$param = array();
					$param['name'] = 'submit_' . $table;
					$param['id'] = 'submit_' . $table;
					$param['value'] = 'Đồng ý';
					echo form_submit($param);
					echo form_reset(array('name'=>'reset','id'=>'reset','value'=>'Làm lại'));
					?>
                </div>
            </td>
        </tr>
<?php
	echo form_close();
?>
</table>
</div>